#include <iostream>
#include <string>
#include <clocale>
#include <fstream>
#include "..\Course\hr_time.h"
#include "Node.h"
#include "List.h"

using namespace std;

using namespace U4;

void updateList(List &l, string s)
{
	Node *n;
	if ((n = l.search(s)) != nullptr)
	{
		n->antal++;
	}
	else
	{
		l.add_at_tail(s);
	}
}

int mainU4()
{
	List *l = new List(100000);
	List *li = new List(10);
	//cout << ("Initiation done.") << endl;

	//Small list for testing move-to-front
	cout << ">>>>> Move to front-list <<<<<" << endl;
	fstream fiin;
	fiin.open("small_list.txt");
	string str;
	while (fiin.good())
	{
		fiin >> str;
		updateList(*li, str);
	}
	fiin.close();
	li->print();
	cout << endl;
	li->searchAndMoveToFront("F");
	li->print();

	//Reading file
	cout << endl << ">>>>> Nils Holgerssons underbara resa <<<<<" << endl;
	cout << "Reading file... ";
	fstream fin;
	fin.open("nils_holgersson.txt");
	string s;
	while (fin.good())
	{
		fin >> s;
		updateList(*l, s);
	}
	fin.close();
	cout << "Done reading file." << endl << endl;

	//Searching for the most frequent word using list class
	string ord;
	int antal;
	antal = l->findMax(ord);
	cout << ">>>>> Count using unordered list class <<<<<" << endl;
	cout << "The word " << ord << " appears " << antal << " times in the text." << endl << endl;


	// Searching for the most frequent word using iterator class
	int max = 0;
	string word;
	for (auto it = l->begin(); it != l->end(); it++)
	{
		if ((*it).antal > max)
		{
			max = (*it).antal;
			word = (*it).data;
		}
	}
	cout << ">>>>> Count using iterator <<<<< " << endl;
	cout << "The word " << word << " appears " << max << " times in the text." << endl;

	delete l;

	return 0;
}

int main()
{

	setlocale(LC_ALL, "swedish");

	mainU4();

	system("PAUSE");
	return 0;
}